<template>
  <div class="app-container">
    <el-form :inline="true" :model="page" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="page.name" size="small" placeholder="请输入标题" clearable />
      </el-form-item>
      <el-form-item>
        <el-select v-model="page.status" size="small" placeholder="处理进度">
          <el-option label="全部" value="" />
          <el-option label="询价" value="0" />
          <el-option label="已报价" value="1" />
          <el-option label="确认付款" value="2" />
          <el-option label="待购买" value="3" />
          <el-option label="购买确认" value="4" />
          <el-option label="等待入库通知" value="5" />
          <el-option label="入库确认" value="6" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="fetchData">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="list"
      size="mini"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        type="index"
        label="ID"
        width="50"
        align="center"
      />
      <el-table-column label="标题" align="center" prop="name" />
      <el-table-column label="图片" align="center" prop="img">
        <template slot-scope="scope">
          <el-image style="width: 20px; height: 20px" :src="scope.row.images" lazy :preview-src-list="[scope.row.images]" />
        </template>
      </el-table-column>
      <el-table-column label="跳转" align="center" prop="url">
        <template slot-scope="scope">
          <el-link :href="scope.row.url">查看<i class="el-icon-view el-icon--right" /></el-link>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag size="mini">{{ statusFormatter(scope.row) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column class-name="status-col" label="操作" align="center" width="250px">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status !== 6"
            size="mini"
            @click="handleEdit(scope.row)"
          >处理
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 20px"
      :hide-on-single-page="page.singlePage"
      :current-page.sync="page.current"
      :page-sizes="[5,10,20,50]"
      :page-size="page.size"
      layout="total,sizes, prev, pager, next"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      :title="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="centerDialogVisible"
      center
    >
      <!--1代买-->
      <el-steps :active="manager.step.split(',').length" align-center finish-status="success">
        <el-step title="询价" :status="manager.step.indexOf('0') >= 0 ?'success':'wait'" />
        <el-step title="已报价" :status="manager.step.indexOf('1') >= 0 ?'success':'wait'" />
        <el-step title="确认付款" :status="manager.step.indexOf('2') >= 0 ?'success':'wait'" />
        <el-step title="待购买" :status="manager.step.indexOf('3') >= 0 ?'success':'wait'" />
        <el-step title="购买确认" :status="manager.step.indexOf('4') >= 0 ?'success':'wait'" />
        <el-step title="等待入库通知" :status="manager.step.indexOf('5') >= 0 ?'success':'wait'" />
        <el-step title="入库确认" :status="manager.step.indexOf('6') >= 0 ?'success':'wait'" />
      </el-steps>
      <span slot="footer" class="dialog-footer">
        <el-button v-for="b in btns" :key="b.code" size="mini" type="primary" @click="refresh(b)"> {{ b.name }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { agentBuyList, merchantEdit, merchantAdd, merchantDel, agentBuyDeal } from '@/api/admin'
export default {
  data() {
    return {
      list: [],
      listLoading: true,
      centerDialogVisible: false,
      formLabelWidth: '80px',
      dialogTitle: '新增海外商家',
      manager: {
        id: -1,
        title: '',
        img: '',
        url: '',
        category: '',
        type: '',
        step: ''
      },
      btns: [],
      page: {
        status: '',
        name: '',
        size: 10,
        current: 1,
        total: 1,
        type: 0,
        singlePage: true
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    statusFormatter(row, column) {
      switch (row.status) {
        case 0:
          return '询价'
        case 1:
          return '已报价'
        case 2:
          return '确认付款'
        case 3:
          return '待购买'
        case 4:
          return '购买确认'
        case 5:
          return '等待入库通知'
        case 6:
          return '入库确认'
        default:
          return ''
      }
    },
    itemDetail(item) {
      this.$router.push({ path: '/school/class', query: { school: item.id }})
    },
    fetchData() {
      this.listLoading = true
      agentBuyList(this.page).then(response => {
        this.list = response.body
        this.page.total = response.count
        this.listLoading = false
      })
    },
    handleAdd() {
      this.dialogTitle = '新增海外商家'
      this.manager.id = -1
      this.manager.title = ''
      this.manager.img = ''
      this.manager.url = ''
      this.manager.category = ''
      this.manager.step = ''
      this.centerDialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '订单处理'
      this.manager.id = row.id
      this.manager.title = row.title
      this.manager.img = row.img
      this.manager.url = row.url
      this.manager.category = row.category
      this.manager.type = row.type
      this.manager.step = row.step
      this.manager.status = row.status
      console.log(this.manager)
      this.btns = this.getBtn()
      console.log(this.btns)
      this.centerDialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('是否确认删除该海外商家', '删除标签', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        merchantDel({ id: row.id }).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      })
    },
    handleNewSave() {
      if (this.manager.id === -1) {
        merchantAdd(this.manager).then(response => {
          if (response.code === 200) {
            this.centerDialogVisible = false
            this.fetchData()
            this.$message({
              type: 'success',
              message: '海外商家添加成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      } else {
        merchantEdit(this.manager).then(response => {
          if (response.code === 200) {
            this.centerDialogVisible = false
            this.fetchData()
            this.$message({
              type: 'success',
              message: '海外商家编辑成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      }
    },
    handleSizeChange(val) {
      this.page.size = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.page.current = val
      this.fetchData()
    },
    // 获取上传图片的路径
    getUpLoadImg(val) {
      this.manager.img = val
    },
    refresh(item) {
      agentBuyDeal({ id: this.manager.id, status: item.code }).then(res => {
        this.manager.status = item.code
        this.manager.step = res.body
        this.btns = this.getBtn()
        this.fetchData()
      })
    },
    getBtn() {
      if (this.manager.type === 1) {
        switch (this.manager.status) {
          case 10:
            return [{ 'code': 11, 'name': '接受委托', 'id': this.manager.id }]
          case 11:
            return [{ 'code': 12, 'name': '已付款', 'id': this.manager.id }]
          case 12:
            return [{ 'code': 13, 'name': '拍卖中', 'id': this.manager.id }]
          case 13:
            return [{ 'code': 14, 'name': '拍卖成功', 'id': this.manager.id }, { 'code': 17, 'name': '拍卖失败', 'id': this.manager.id }]
          case 14:
            return [{ 'code': 15, 'name': '等待入库', 'id': this.manager.id }]
          case 15:
            return [{ 'code': 16, 'name': '入库确认', 'id': this.manager.id }]
          case 16:
            return []
          case 17:
            return [{ 'code': 18, 'name': '已退款', 'id': this.manager.id }]
          case 18:
            return []
          default:
            return [{ 'code': 10, 'name': '申请', 'id': this.manager.id }]
        }
      } else if (this.manager.type === 0) {
        switch (this.manager.status) {
          case 0:
            return [{ 'code': 1, 'name': '已报价' }]
          case 1:
            return [{ 'code': 2, 'name': '确认付款' }]
          case 2:
            return [{ 'code': 3, 'name': '待购买' }]
          case 3:
            return [{ 'code': 4, 'name': '购买确认' }]
          case 4:
            return [{ 'code': 5, 'name': '等待入库通知' }]
          case 5:
            return [{ 'code': 6, 'name': '入库确认' }]
          case 6:
            return []
          default:
            return [{ 'code': 0, 'name': '询价' }]
        }
      } else if (this.manager.type === 2) {
        switch (this.manager.status) {
          case 20:
            return [{ 'code': 21, 'name': '初步商检入库' }]
          case 21:
            return [{ 'code': 22, 'name': '收到客户集货指令' }]
          case 22:
            return [{ 'code': 23, 'name': '集货完成' }]
          case 23:
            return [{ 'code': 24, 'name': '发货' }]
          case 24:
            return [{ 'code': 25, 'name': '后续跟单' }]
          case 25:
            return [{ 'code': 26, 'name': '归档' }]
          case 26:
            return []
          default:
            return [{ 'code': 20, 'name': '收到货物' }]
        }
      }
    }
  }
}
</script>
<style>
  .el-step__title{
    font-size: 5px;
  }
</style>
